package com.api.shopapi.mapper;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.RetreatSettle;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RetreatSettleMapper {


    @Insert("insert into retreat_settle " +
            "(" +
            "RetreatSettleCode, " +
            "ElderlyCode, " +
            "ElderlyName, " +
            "Total, " +
            "CreateTime, " +
            "Remarks, " +
            "StaffCode, " +
            "InstitutionCode," +
            "CheckinCode," +
            "ElderlyBalance," +
            "ReturnBlance" +
            ") " +
            "VALUES (" +
            "#{retreatSettle.RetreatSettleCode} ," +
            "#{retreatSettle.ElderlyCode}," +
            "#{retreatSettle.ElderlyName}," +
            "#{retreatSettle.Total}," +
            "#{retreatSettle.CreateTime}," +
            "#{retreatSettle.Remarks}," +
            "#{retreatSettle.StaffCode}," +
            "#{retreatSettle.InstitutionCode}," +
            "#{retreatSettle.CheckinCode}," +
            "#{retreatSettle.ElderlyBalance}," +
            "#{retreatSettle.ReturnBlance}" +
            ")")
    int insert(@Param("retreatSettle") RetreatSettle retreatSettle);


    @Update("update  retreat_settle " +
            "set " +
            "RetreatSettleCode=#{retreatSettle.RetreatSettleCode}, " +
            "ElderlyCode=#{retreatSettle.ElderlyCode}," +
            "ElderlyName=#{retreatSettle.ElderlyName}," +
            "Total=#{retreatSettle.Total}," +
            "CreateTime=#{retreatSettle.CreateTime}," +
            "Remarks=#{retreatSettle.Remarks}," +
            "StaffCode=#{retreatSettle.StaffCode}," +
            "InstitutionCode=#{retreatSettle.InstitutionCode}," +
            "CheckinCode=#{retreatSettle.CheckinCode}," +
            "ElderlyBalance=#{retreatSettle.ElderlyBalance}," +
            "ReturnBlance=#{retreatSettle.ReturnBlance}  " +
            "where " +
            "RetreatSettleCode=#{retreatSettle.RetreatSettleCode} ")
    int update(@Param("retreatSettle") RetreatSettle retreatSettle);

    @Select("<script>" +
            "select * from retreat_settle " +
            "where InstitutionCode = #{institutionCode} " +
            "<if test ='elderlyName !=null and elderlyName!=\"\" '>" +
            "and ElderlyName like concat('%',#{elderlyName},'%') " +
            "</if>" +
            "order by CreateTime desc" +
            "</script>")
    List<RetreatSettle> getByInstitutionCode(@Param("institutionCode") String institutionCode,@Param("elderlyName") String elderlyName);

    @Select("SELECT * FROM retreat_settle WHERE ElderlyCode = #{elderlyCode} order by CreateTime desc")
    List<RetreatSettle> getByElderlyCode(@Param("elderlyCode") String elderlyCode);

    @Select("SELECT SUM(Total) FROM retreat_settle WHERE ElderlyCode = #{elderlyCode}")
    double getSumTotal(@Param("elderlyCode") String elderlyCode);

    @Select("SELECT SUM(Total) sumTotal FROM retreat_settle WHERE InstitutionCode = #{institutionCode}")
    Object getSumTotalByInstitutionCode(@Param("institutionCode") String institutionCode);
}
